package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.conclusion;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.DigitalConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.RiskConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.ThirdPartyConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.CustInfoModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.thirdparty.cbit.CbitResponseModel;
import com.bmps.springcloud.insuredmicroservice.model.thirdparty.transit.CloudResponseModel;
import com.bmps.springcloud.insuredmicroservice.model.thirdparty.transit.ConclusionModel;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * description:
 * 调用保盾云，大于等于600分直接拒保；调用中保信，“重疾理赔”字段为Y的，直接拒保；不调用百融
 *
 * @author jackdaw
 * @date 2020-06-29 14:53
 */
@Component
public class Hct010ConclusionStrategy implements ConclusionStrategy {

    @Override
    public ConclusionModel underwrite(ConclusionModel conclusion, RequestModel requestModel, boolean artificial) {
        CustInfoModel custInfoModel = requestModel.getCustInfo();
        CloudResponseModel cloud = conclusion.getCloud();
        //保盾云分数
        double info = cloud == null ? 0 : cloud.getInfo();
        if (info > DigitalConstant.SIX_HUNDRED) {
            conclusion.setConclusion(1);
            conclusion.setMessage(String.format(AcceptConstant.CLOUD_DECLINATURE_MESSAGE, AcceptUtil.getCustomerName(custInfoModel, conclusion.getId())));
            return conclusion;
        } else {
            List<CbitResponseModel> cbitList = conclusion.getCbits();
            if (CollectionUtils.isNotEmpty(cbitList)) {
                for (CbitResponseModel cbitResponseModel : cbitList) {
                    if (StringUtils.isBlank(cbitResponseModel.getCode()) &&
                            StringUtils.equals(ThirdPartyConstant.CBIT_SUCCESS, cbitResponseModel.getRetCode())) {
                        JSONObject data = JSON.parseObject(cbitResponseModel.getData());
                        for (String key : data.keySet()) {
                            boolean flag = StringUtils.equals(ThirdPartyConstant.RISK_SCORE, key) && Double.parseDouble(data.getString(key)) >= 85 ||
                                    StringUtils.equals(ThirdPartyConstant.MAJOR_DISEASE_PAYMEN, key) && StringUtils.equals(ThirdPartyConstant.YES, data.getString(key));
                            if (flag) {
                                conclusion.setConclusion(1);
                                conclusion.setMessage(String.format(AcceptConstant.CBIT_DECLINATURE_MESSAGE, AcceptUtil.getCustomerName(custInfoModel, conclusion.getId())));
                                return conclusion;
                            }
                        }
                    }
                }
            }
        }
        conclusion.setConclusion(2);
        return conclusion;
    }
}
